package online.longzipeng.householdbillssystem.common.utils;

import online.longzipeng.householdbillssystem.common.exception.AppException;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: lzp
 * @Date: 2021/2/8 13:49
 * 自定义异常工具类
 */
public class ExceptionUtils {

    /**
     * 不否和要求就抛出自定义异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isTrue(boolean expression,String message,Object data){
        if (!expression) {
            throw new AppException("750",message,data);
        }

    }

    public static void isTrue(boolean expression,String message){
        if (!expression) {
            throw new AppException("750",message,null);
        }

    }


    /**
     * 如果为不为null就抛出异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isNull(Object expression,String message,Object data){
        if (expression != null) {
            throw new AppException("750",message,data);
        }

    }

    public static void isNull(List expression,String message,Object data){
        if (!CollectionUtils.isEmpty(expression)) {
            throw new AppException("750",message,data);
        }

    }

    public static void isNull(Object expression, ResponseEnum responseEnum, Object data){
        if (expression != null) {
            throw new AppException(responseEnum.getCode(),responseEnum.getMessage(),data);
        }

    }

    public static void isNull(List expression, ResponseEnum responseEnum, Object data){
        if (!CollectionUtils.isEmpty(expression)) {
            throw new AppException(responseEnum.getCode(),responseEnum.getMessage(),data);
        }

    }

    public static void isNull(Object expression,String message){
        if (expression != null) {
            throw new AppException("750",message,null);
        }

    }

    public static void isNull(String expression,String message){
        if (!StringUtils.isEmpty(expression)) {
            throw new AppException("750",message,null);
        }

    }

    public static void isNull(List expression, String message){
        if (!CollectionUtils.isEmpty(expression)) {
            throw new AppException("750",message,null);
        }

    }

    /**
     * 如果为null就抛出异常
     * @param expression
     * @param message
     * @param data
     */
    public static void isNotNull(Object expression,String message,Object data){
        if (expression == null) {
            throw new AppException("750",message,data);
        }

    }

    public static void isNotNull(List expression,String message,Object data){
        if (CollectionUtils.isEmpty(expression)) {
            throw new AppException("750",message,data);
        }

    }

    public static void isNotNull(Object expression, ResponseEnum responseEnum, Object data){
        if (expression == null) {
            throw new AppException(responseEnum.getCode(),responseEnum.getMessage(),data);
        }

    }

    public static void isNotNull(List expression, ResponseEnum responseEnum, Object data){
        if (CollectionUtils.isEmpty(expression)) {
            throw new AppException(responseEnum.getCode(),responseEnum.getMessage(),data);
        }

    }

    public static void isNotNull(Object expression,String message){
        if (expression == null) {
            throw new AppException("750",message,null);
        }

    }

    public static void isNotNull(String expression,String message){
        if (StringUtils.isEmpty(expression)) {
            throw new AppException("750",message,null);
        }

    }

    public static void isNotNull(List expression,String message){
        if (CollectionUtils.isEmpty(expression)) {
            throw new AppException("750",message,null);
        }

    }
}
